package com.atguigu.gulimall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author sunbaojin
 * @date 2020/5/24 5:56
 */
@Configuration
public class GatewayCorsConfig {

    @Bean
    public CorsWebFilter getCorsWebFilter() {

        //  CorsConfigurationSource 有个子类UrlBasedCorsConfigurationSource，就实例化它
        // 注意：都要使用 org.springframework.web.cors.reactive下的包
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource =
                new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 设置跨域信息
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        // 携带cookie
        corsConfiguration.setAllowCredentials(true);

        // 配置拦截路径
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
